﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Auditors_BS : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        StringBuilder sb = new StringBuilder();

        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];
        var v = new CompanyBLL().GetCompanyByCompanyid(company);

        var AuditorsEntity = new CompanyBLL().GetCompanyAuditorsEntityByOrderId(orderid);

        if (AuditorsEntity == null)
        {
            AlertMsg("该合数信息异常（获取不到核数起止日期），请联系管理员。");
            return;
        }

        List<CompanyDetailEntity> CompanyDetailList = new AuditorsBLL().GetCompanyDetail(company, orderid);

        double cashAtBank = Math.Round(new AuditorsBLL().GetCashAtBank(company, orderid), 2);
        double currentLiabilities = 0;

        int RetainLosses = GetRetainLosses();
        double other = 0;

        sb.AppendFormat("<div>{0}</div>", v.EnglishName);
        sb.AppendFormat("<div>From the period from {0} to {1}</div>", AuditorsEntity.StartTime.ToString("dd/MM/yyyy"), AuditorsEntity.EndTime.ToString("dd/MM/yyyy"));
        sb.Append("<div>Balance sheet</div>");


        sb.Append("<table>");
        sb.Append("<tr>");
        sb.Append("<td></td>");
        sb.AppendFormat("<td>{0}</td>", AuditorsEntity.StartTime.Year);
        sb.AppendFormat("<td>{0}</td>", AuditorsEntity.StartTime.Year - 1);
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td></td>");
        sb.Append("<td>HK</td>");
        sb.Append("<td>HK</td>");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td>Current assets</td>");
        sb.Append("<td></td>");
        sb.Append("<td></td>");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td>Cash at bank</td>");
        sb.AppendFormat("<td>{0}</td>", cashAtBank);
        sb.AppendFormat("<td>{0}</td>", "");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td></br></td>");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td>Current liabilities</td>");
        sb.Append("<td></td>");
        sb.Append("<td></td>");
        sb.Append("</tr>");

        foreach (var companyDetailEntity in CompanyDetailList.Where(fun => fun.TypeProviderid == 2 && fun.Kinds == 1))
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", companyDetailEntity.EnglishName);
            sb.AppendFormat("<td>{0}</td>", companyDetailEntity.Amount > 0 ? companyDetailEntity.Amount.ToString() : "");
            sb.AppendFormat("<td>{0}</td>", "");
            sb.Append("</tr>");

            currentLiabilities += companyDetailEntity.Amount;
        }

        sb.Append("<tr>");
        sb.Append("<td></td>");
        sb.AppendFormat("<td>{0}</td>", Math.Round(currentLiabilities, 2));
        sb.AppendFormat("<td>{0}</td>", "");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td></br></td>");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td>Net current asset</td>");
        sb.AppendFormat("<td>{0}</td>", Math.Round((cashAtBank - currentLiabilities), 2));
        sb.AppendFormat("<td>{0}</td>", "");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td></br></td>");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td>Capital and reserve</td>");
        sb.Append("<td></td>");
        sb.Append("<td></td>");
        sb.Append("</tr>");


        foreach (var companyDetailEntity in CompanyDetailList.Where(fun => fun.TypeProviderid == 2 && fun.Kinds == 2))
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", companyDetailEntity.EnglishName);
            sb.AppendFormat("<td>{0}</td>", companyDetailEntity.Amount > 0 ? companyDetailEntity.Amount.ToString() : "");
            sb.AppendFormat("<td>{0}</td>", "");
            sb.Append("</tr>");

            other += companyDetailEntity.Amount;
        }

        sb.Append("<tr>");
        sb.Append("<td>Retain losses</td>");
        sb.AppendFormat("<td>{0}</td>", RetainLosses);
        sb.AppendFormat("<td>{0}</td>", "");
        sb.Append("</tr>");

        sb.Append("<tr>");
        sb.Append("<td></td>");
        sb.AppendFormat("<td>{0}</td>", Math.Round(other + RetainLosses, 2));
        sb.AppendFormat("<td>{0}</td>", "");
        sb.Append("</tr>");

        sb.Append("</table>");

        Label1.Text = sb.ToString();
    }


    int GetRetainLosses()
    {
        string company = Request.QueryString["CompanyID"];
        string orderid = Request.QueryString["orderid"];

        List<CompanyDetailEntity> CompanyDetailList = new AuditorsBLL().GetCompanyDetail(company, orderid);

        int Revenue = new AuditorsBLL().GetRevenue(company, orderid);

        int cost = new AuditorsBLL().GetCost(company, orderid);

        double ProfitTax = new AuditorsBLL().GetProfit(company, orderid) * 0.165;

        int sum = Revenue - cost;

        foreach (var companyDetailEntity in CompanyDetailList.Where(fun => fun.TypeProviderid == 1))
        {
            sum -= Convert.ToInt32(companyDetailEntity.Amount);
        }

        sum = (int)(sum - ProfitTax);

        return sum;
    }
}